package cn.afactory.tygz.model;

import android.widget.BaseAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

import cn.afactory.tygz.common.ErrorCode;

/**
 * Created with Android Studio.
 * Title:SecondGoodsInfo
 * Description:
 * Copyright:Copyright (c) 2015
 * Company:和平必胜、正义必胜、人民必胜
 * Author:炎黄子孙
 * Date:15/9/11
 * Time:下午3:44
 * Version 1.0
 */
public class VersionInfo extends BaseDataHeader implements Serializable{
    private static final long serialVersionUID = 3246451692600706144L;

    public long versionCode;

    public String versionDes;

    public String downloadUrl;

    public boolean isForceUpgrade = false;

    /**
     * 反序列化函数，用于从json字符串反序列化本类型实例
     */
    public static VersionInfo deserialize(String json) throws JSONException {
        if (json != null && !json.isEmpty()) {
            return deserialize(new JSONObject(json));
        }
        return null;
    }

    /**
     * 反序列化函数，用于从json节点对象反序列化本类型实例
     */
    public static VersionInfo deserialize(JSONObject json) throws JSONException {
        if (json != null && json != JSONObject.NULL && json.length() > 0) {
            VersionInfo result = new VersionInfo();

            result.message = json.optString("message");

            result.state = json.optInt("state");

            JSONObject jsonObject = json.optJSONObject("data");
            if(result.state == ErrorCode.STATUS_OK) {
                if (jsonObject != null) {
                    json = jsonObject.optJSONObject("value");
                    if(json != null) {
                        result.versionCode = json.optInt("versionCode");

                        if (!json.isNull("versionDes")) {
                            result.versionDes = json.optString("versionDes", null);
                        }

                        if (!json.isNull("downloadUrl")) {
                            result.downloadUrl = json.optString("downloadUrl", null);
                        }
                    }
                }
            }
            return result;
        }
        return null;
    }
}
